#pragma once

/*
A class that represents a matrix. This class stores cells in coloumn-wise fashion just like how 
matlab stores, so it's safe to just copy the storage over to be used in matlab. 
*/
#include <assert.h>

namespace matlab{

template <class T>
class MatlabMatrix
{
public:
	MatlabMatrix()
	{
		nRow = -1; 
		nCol = -1; 
		storage = 0; 
	}

	MatlabMatrix(int row, int col)
	{
		storage = 0; 
		init(row, col); 
	}

	~MatlabMatrix(void)
	{
		if (storage)
			delete[] storage; 
	}

	void init(int row, int col)
	{
		nRow = row; 
		nCol = col; 
		storage = new T [row*col]; 
	}


	T&  operator()(const int row, const int col)
	{
		assert(row>=0 && row <nRow);
		assert(col>=0 && col < nCol);
	 
		int index = col*nRow + row; 
		return storage[index];
	}

	T* getStorage()const {return storage; }


	const MatlabMatrix<T>& operator=(const MatlabMatrix<T>& rhs)
	{
		if (storage)
			delete[] storage; 
		nRow = rhs.nRows(); 
		nCol = rhs.nCols(); 

		storage = new T[nRow*nCol]; 
		memcpy(storage, rhs.storage, nRow*nCol*sizeof(T));  

		return *this; 
	}

	int nRows()const {return nRow; }
	int nCols()const {return nCol; }

protected:
	int nRow; 
	int nCol; 
	T* storage; 
};
}
